
import { login, getUserInfo, getUserInfoById } from '@/api/user.js'
import { getToken, setToken, removeToken, timeStampToken } from '@/utils/auth.js'

const state = {
  token: getToken(),
  userInfo: {}
}

const mutations = {
  // 存token
  StoreUrer(state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 存
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  // 删
  logeRemov(state, payload) {
    state.token = null
    state.userInfo = {}
    removeToken()
  }
}

const actions = {
  // 登录
  async login(context, payload) {
    const res = await login(payload)
    context.commit('StoreUrer', res)
    timeStampToken()
  },
  // 获取用户资料
  async getUserInfo(context, payload) {
    console.log('获取用户信息')
    const res = await getUserInfo()
    console.log(res)
    // 获取详情
    const getById = await getUserInfoById(res.userId)
    console.log(getById)
    context.commit('setUserInfo', { ...res, ...getById })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

